package com.visionblue.business.ship.page.register.config

import androidx.lifecycle.MutableLiveData
import com.visionblue.business.ship.base.BaseRepository
import com.visionblue.business.ship.model.repository.db.MeasureMenuItem
import com.visionblue.business.ship.model.repository.db.MeasureMenuItemDao

/**
 * @author: xlh
 * @date: 2020-07-21 5:04 PM
 * @desc:
 */
class MenuConfigRepository(private val measureMenuItemDao: MeasureMenuItemDao) : BaseRepository() {

//    init {
//        getMenuItems()
//    }

    suspend fun getMenuItems(): List<MeasureMenuItem> {
        makeNotEmpty()
        return measureMenuItemDao.getAll()
    }

    private suspend fun makeNotEmpty() {
        val menuItems = measureMenuItemDao.getAll()
        //todo: 这块预填数据应该要放到数据库升级中去
        menuItems.takeIf { it.size < 8 }?.run {
            measureMenuItemDao.delete(*menuItems.toTypedArray())
            insert(
                arrayOf(
                    MeasureMenuItem(WATER_VOLUME_NAME, "生活污水管"),
                    MeasureMenuItem(WATER_VOLUME_NAME2, "含油污水管"),
                    MeasureMenuItem(OIL_VOLUME_NAME, "废机油管"),
                    MeasureMenuItem(ULTRASONIC_NAME, "自定义名称", isChecked = false),
                    MeasureMenuItem(WEIGHT_NAME, "称重"),
                    MeasureMenuItem(WATER_VOLUME_NAME, "生活污水管", isOutput = true),
                    MeasureMenuItem(WATER_VOLUME_NAME2, "含油污水管", isOutput = true),
                    MeasureMenuItem(OIL_VOLUME_NAME, "废机油管", isOutput = true),
                    MeasureMenuItem(ULTRASONIC_NAME, "自定义名称", isChecked = false, isOutput = true),
                    MeasureMenuItem(WEIGHT_NAME, "称重", isOutput = true)
                )
            )
        }
    }

    suspend fun getMenuItemsByOutput(isOutput: Boolean): List<MeasureMenuItem> {
        makeNotEmpty()
        return measureMenuItemDao.getAllByCondition(isOutput, true)
    }


    suspend fun insert(itemArray: Array<MeasureMenuItem>) {
        measureMenuItemDao.insert(*itemArray)
    }

    suspend fun update(measureMenuItem: MeasureMenuItem) {
        measureMenuItemDao.update(measureMenuItem)
    }

    suspend fun getMeasureMenuItemByName(name: String): MeasureMenuItem {
        return measureMenuItemDao.getMeasureMenuItemByName(name)
    }


}